<template>
  <div class="app-container calendar-list-container">
   <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="请输入角色名称" v-model="search.name"></el-input>
      <el-button class="filter-item" type="primary" plain v-waves icon="el-icon-search" @click="handleFilter">搜索</el-button>
     <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" v-waves type="primary" v-roles="'editRole'"  icon="el-icon-edit">添加角色</el-button>
    </div>
    <!-- // 这是列表 -->
    <el-table :data="listData" v-loading="listLoading" element-loading-text="给我一点时间" border fit highlight-current-row style="width: 100%">
      <el-table-column type="index"  align="center" label="序号" width="80"></el-table-column>
      <el-table-column align="center" label="角色名称">
        <template slot-scope="scope">
          <span>{{scope.row.name}}</span>
        </template>
      </el-table-column>
      <!-- //这里是操作   -->
      <el-table-column align="center" label="操作" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handlEdit(scope.row)" v-roles="'editRole'">编辑</el-button>
          <el-button v-if="scope.row.id !=1" size="mini" type="danger" @click="handlDel(scope.row)" v-roles="'deleteRole'" >删除</el-button>
          <el-button size="mini" type="info" @click="handlInfo(scope.row)">查看用户</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--可自定义按钮的样式、show/hide临界点、返回的位置  -->
    <!--如需文字提示，可在外部添加element的<el-tooltip></el-tooltip>元素  -->
    <el-tooltip placement="top" content="回到顶部">
      <back-to-top transitionName="fade" :customStyle="myBackToTopStyle" :visibilityHeight="300" :backPosition="50"></back-to-top>
    </el-tooltip>
  </div>
</template>

<script>

import { mapGetters } from 'vuex'
import BackToTop from '@/components/BackToTop' // 回到顶部
import * as api from '@/api/main-api' // API
import waves from '@/directive/waves' // 水波纹指令

export default {
  name: 'rolesIndex',

  directives: {
    waves
  },

  components: { BackToTop },
  data() {
    return {
      // 回到顶部样式
      myBackToTopStyle: {
        right: '50px',
        bottom: '50px',
        width: '40px',
        height: '40px',
        'border-radius': '4px',
        'line-height': '45px', // 请保持与高度一致以垂直居中 Please keep consistent with height to center vertically
        background: '#e7eaf1'// 按钮的背景颜色 The background color of the button
      },
      // 搜索字段
      search: {
        name: ''
      },

      listLoading: false,
      listData: []
    }
  },

  computed: {
    ...mapGetters([
      //     'roles'
    ])
  },

  // 初始化数据
  created() {
    this.fetchData()
  },

  // 触动方法
  methods: {

    // 搜搜
    handleFilter() {
      this.fetchData()
    },

    // 添加
    handleCreate() {
      this.$nextTick(() => {
        this.$router.push({
          path: '/permission/edit-role',
          name: 'editRole',
          query: { es: 'add' }
        })
      })
    },

    // 子窗口关闭,刷新数据
    isrefresh(data) {
      this.fetchData()
    },

    // 编辑
    handlEdit(row) {
      // 存在 vuex session
      this.$store.dispatch('setRoleInfo', row)
      this.$nextTick(() => {
        this.$router.push({
          path: '/permission/edit-role',
          name: 'editRole',
          query: { es: 'edit' }
        })
      })
    },

    // 查看用户
    handlInfo(row) {
      this.$nextTick(() => {
        this.$router.push({
          path: '/permission/permission-admin',
          name: 'permissionAdmin',
          query: { es: 'looking', role_id: row.id } // 权限 id
        })
      })
    },

    // 删除
    handlDel(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var params = {
          id: row.id
        }
        api.deleteRoleApi(params).then(response => {
          if (response.data.code === 0) {
            const index = this.listData.indexOf(row)
            this.listData.splice(index, 1)
            this.$root.message('已移除数据', 'success')
            this.total = this.total - 1
          }
        })
      }).catch(() => {
        this.$root.message('已取消删除', 'info')
      })
    },

    // 获取数据
    fetchData() {
      this.listLoading = true
      return new Promise((resolve, reject) => {
        // 获取角色列表
        api.roleListApi(this.search).then(response => {
          if (response.data.code === 0) {
            this.listData = response.data.data
          }
          this.listLoading = false
        })
      })
    }

  }
}
</script>
